<script setup>
import InputEditer from '../components/InputEditer.vue'
import InputEditerArrText from '../components/InputEditerArrText.vue'
import InputEditerArr from '../components/InputEditerArr2.vue'
import { ref } from 'vue'
import { _getData, _getFileData, _putData, _setFilePath } from '/src/api/parameterConfiguration/getData.js'
import { mainStore } from '/src/store/mainStore.js'
import { dataProcessing_deep } from '../js/dataPro_deep.js'
import DeepstreamPop from './DeepstreamPop.vue'
import { _saveItem } from '/src/api/parameterConfiguration/index.js'

const store = mainStore()

let data_1 = ref(null)
let data_2 = ref(null)
let data_3 = ref(null)
let fileBtns = ref([])
let inputFile = ref('')
let showPop = ref(false)
const getList = async () => {
  const res = await _getData(store.cutArgPage)
  if (res?.data?.code != 200) return ElMessage.error('获取数据失败')

  const data = dataProcessing_deep(res.data.data)

  data_1.value = data.data_1
  data_2.value = data.data_2
  data_3.value = data.data_3
  fileBtns.value = res.data.data.deepstream.pipeline

  fileBtns.value = fileBtns.value.map((item) => ({ name: item }))
}

const addFile = async () => {
  inputFile.value.length <= 0 && ElMessage.error('请输入文件名称')
  const params = fileBtns.value.find((item) => item.name == inputFile.value)
  if (params) return ElMessage.error('文件名称已存在')
  // 调接口添加文件
  fileBtns.value.push({ name: inputFile.value })
  const stringArray = fileBtns.value.map((item) => item.name)
  const param = {
    deepstream: {
      pipeline: stringArray,
    },
  }

  const res = await _putData(param)

  if (res.data.code == 200) {
    ElMessage.success('添加成功')
  } else {
    ElMessage.error('添加失败')
  }
}

getList()

let pop_1_data = ref(null)
let currentName = ref('')
const handleClick = async (row) => {
  const params = {
    'deepstream-config-name': row.name,
  }
  currentName.value = row.name
  await _setFilePath('deep-stream-config', params)
  const res = await _getFileData('deep-stream-config')
  if (res.data.code == 200) {
    showPop.value = true
    pop_1_data.value = res.data.data
  } else {
    ElMessage.error('获取文件信息失败')
  }
}
let reload = ref(0)
const query = async () => {
  await handleClick({ name: currentName.value })
  reload.value = Date.now()
  ElMessage.success('获取文件信息成功')
}

const deleteItem = async (row) => {
  fileBtns.value = fileBtns.value.filter((item) => item.name != row.name)
  const params = {
    deepstream: {
      pipeline: fileBtns.value.map((item) => item.name),
    },
  }
  const res = await _saveItem('deep-stream-application', params)
  if (res.data.code == 200) ElMessage.success('删除成功')
  if (res.data.code != 200) ElMessage.error('删除失败')
}

const close = () => {
  showPop.value = false
}
</script>

<template>
  <div class="deep-container">
    <div class="deep-item" v-if="data_1">
      <div class="input-box">
        <div class="title">{{ data_1.title }}</div>
        <div class="input-item" v-for="item in data_1?.data" :key="item">
          <InputEditer :data="item" :width="200" :inputWidth="360 + 'px'" />
        </div>
      </div>
    </div>

    <div class="deep-item" v-if="data_2">
      <div class="input-box">
        <div class="title">{{ data_2.title }}</div>
        <div class="input-item" v-for="item in data_2?.data" :key="item">
          <InputEditerArrText v-if="item.showType == 'arrInput'" :data="item" :width="200" />
          <InputEditer v-else :data="item" :width="200" :inputWidth="360 + 'px'" />
        </div>
      </div>
    </div>

    <div class="deep-item" v-if="data_3">
      <div class="title">{{ data_3.title }}</div>
      <div class="input-item" style="margin-top: 10px" v-for="item in data_3?.data" :key="item">
        <InputEditerArr v-if="item.arr" :data="item" :width="200" />
        <InputEditer v-else :data="item" :width="200" :inputWidth="360 + 'px'" />
      </div>
    </div>

    <!-- 文件按钮 -->
    <div class="file-ben-box">
      <div class="add">
        <p>添加文件:</p>
        <el-input type="text" v-model="inputFile" style="width: 300px; margin-left: 20px" size="small"></el-input>
        <img src="/public/parameterConfig/add.png" @click="addFile" />
      </div>
      <el-table :data="fileBtns" border style="width: 100%">
        <el-table-column prop="name" label="文件名称" />
        <el-table-column label="操作" width="180">
          <template #default="scope">
            <div class="cz-box">
              <p @click="handleClick(scope.row)">查看</p>
              <p @click="deleteItem(scope.row)">删除</p>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>

  <!-- 弹出层 -->
  <div class="mask" v-if="showPop" @click="close">
    <DeepstreamPop :data="pop_1_data" :name="currentName" :query="query" :close="close" :key="reload"></DeepstreamPop>
  </div>
</template>

<style lang="less" scoped>
.mask {
  width: 100%;
  height: 100%;
  position: fixed;
  top: 0;
  z-index: 9;
  backdrop-filter: blur(10px);
  // display: none;
  display: flex;
  justify-content: center;
}
.deep-container {
  padding: 10px;
  font-size: 14px;
}
.input-box {
  display: flex;
  width: 100%;
  margin-top: 10px;
  .input-item {
    // width: 600px;
    margin-right: 40px;
  }
}
.deep-item {
  margin-top: 10px;
}

.cz-box {
  display: flex;
  > p {
    cursor: pointer;
    color: blue;
    margin-right: 20px;
  }
}

.file-ben-box {
  width: 600px;
  margin-top: 20px;
  .add {
    height: 30px;
    margin-bottom: 20px;
    display: flex;
    align-items: center;
    img {
      width: 20px;
      height: 20px;
      cursor: pointer;
      margin-left: 12px;
    }
  }
}

.title {
  width: 100px;
  color: blueviolet;
  font-size: 16px;
  font-weight: 600;
}
</style>
